    //
//  SettingPollingViewController.m
//  PhotoFrame
//
//  Created by William on 11-5-7.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SettingPollingViewController.h"
#import "GlobalSettingUtils.h"

@implementation SettingPollingViewController
@synthesize picker;
@synthesize firstDigitals;
@synthesize secondDigitals;
@synthesize myTable;
@synthesize mainContent;
@synthesize delegate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
								   initWithTitle:@"Done" 
								   style:UIBarButtonItemStyleBordered 
								   target:self action:@selector(closePop)];
	self.navigationItem.rightBarButtonItem = doneButton;
	[doneButton release];
	
	self.firstDigitals = [[NSArray alloc] initWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil];
	self.secondDigitals = [[NSArray alloc] initWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil]; 
	
	// load properties from user document folder
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* sandBoxpath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"globalconf.plist"];
	NSLog(@"%@", sandBoxpath);
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:sandBoxpath];
	
	// load from app default resources folder
	if (dict == nil) {
		NSString *path = [[NSBundle mainBundle] pathForResource:@"globalconf" ofType:@"plist"];
		dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	}
	
	NSString *polling = [NSString stringWithFormat:@"%2@", [dict objectForKey:@"polling"]];
	NSString *units = [polling substringWithRange:NSMakeRange(0, 1)];
	NSString *tens = [polling substringWithRange:NSMakeRange(1, 1)];
	
	[self.picker selectRow:[units intValue] inComponent:0 animated:YES];
	[self.picker selectRow:[tens intValue] inComponent:1 animated:YES];
	[self.picker selectedRowInComponent:0];
	
	[dict release];
    [super viewDidLoad];
}

- (void) closePop {
	[delegate closePopover];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.picker = nil;
	self.firstDigitals = nil;
	self.secondDigitals = nil;
	self.myTable = nil;
	self.mainContent = nil;
}


- (void)dealloc {
	[picker release];
	[firstDigitals release];
	[secondDigitals release];
	[myTable release];
	[mainContent release];
    [super dealloc];
}

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return 2;
}

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {	
	return component == 0 ? [self.firstDigitals count] : [self.secondDigitals count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	if (component == 0) {
		return [self.firstDigitals objectAtIndex:row];
	} else {
		return [self.secondDigitals objectAtIndex:row];
	}
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	NSInteger firstDigital = [picker selectedRowInComponent:0];
	NSInteger secondDigital = [picker selectedRowInComponent:1];
	
	NSString *newPollingInterval = [NSString stringWithFormat:@"%d%d", firstDigital, secondDigital];
	[GlobalSettingUtils setGlobalConfForValue:newPollingInterval forKey:@"polling"];
	
	NSString *value = [NSString stringWithFormat:@"%d秒", [newPollingInterval intValue]];
	mainContent.text = value;
	
	[self.delegate setPhotoPollingTimerInterval:[newPollingInterval intValue]];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"設定";
}

- (NSInteger)numberOfSections {
    return 1;
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *identify = @"SimpleTableIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] 
				 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
	}
	
	NSString *polling = [[GlobalSettingUtils getGlobalConf] objectForKey:@"polling"];
	NSString *value = [NSString stringWithFormat:@"%d秒", [polling intValue]];
	cell.textLabel.text = @"トランジション";
	
	UILabel *contentView = [[UILabel alloc] initWithFrame:CGRectMake(200, 0, 120, 43)];
	[contentView setFont:[UIFont systemFontOfSize: 17.0f]];
	contentView.textColor = [UIColor colorWithRed:87.0/255.0 green:158.0/255.0 blue:212.0/255.0 alpha:1];
    contentView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
	[contentView setTextAlignment:UITextAlignmentRight];
	contentView.text = value;
	self.mainContent= contentView;
	[cell.contentView addSubview:mainContent];
	[contentView release];
	return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
